package com.liuwei.assemble.services.impl;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.liuwei.assemble.common.GIOUtils;
import com.liuwei.assemble.constants.PathConstants;
import com.liuwei.assemble.dtos.GeneratorContextDTO;
import com.liuwei.assemble.services.IGeneratorProject;
import com.liuwei.assemble.vo.TableFieldVO;
import com.liuwei.assemble.vo.TableInfoVO;

import org.apache.commons.io.FileUtils;
import org.apache.velocity.VelocityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author liuwei19
 */
public class BootStrapGenerator implements IGeneratorProject {
    private static final Logger logger = LoggerFactory.getLogger(BootStrapGenerator.class);
    @Override
    public void generator(GeneratorContextDTO generatorParam) throws Exception {
        File file = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+"/src/main/resources/static");
        File sourceFile = ResourceUtils.getFile("classpath:velocity/springboot/resources/static");
        
        //拷贝静态文件
        GIOUtils.copyFolder(sourceFile,new File(file.getPath()));
        
        //拷贝公用layout文件
        File frame = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+"/src/main/resources/templates/frame");
        File frameSource= ResourceUtils.getFile("classpath:velocity/springboot/resources/templates/frame");
        GIOUtils.copyFolder(frameSource,frame);
        
        doVelocity(generatorParam);
    }
    
    private void doVelocity(GeneratorContextDTO generatorParam) throws IOException {
        VelocityContext context = new VelocityContext();
        context.put("projectName",generatorParam.getProjectName());
        List<TableInfo> tableInfos = generatorParam.getTableInfos();
//        List<TableInfoVO> tableInfoVOList = generatorParam.getTableInfos().stream().map(table->{
//            TableInfoVO tableInfoVO = new TableInfoVO();
//            BeanUtils.copyProperties(table,tableInfoVO);
//            
//            List<TableFieldVO> fieldVOList = table.getFields().stream().map(field->{
//                TableFieldVO fieldVO = new TableFieldVO();
//                BeanUtils.copyProperties(field,fieldVO);
//                return fieldVO;
//            }).collect(Collectors.toList());
//            
//            tableInfoVO.setTableFieldVOList(fieldVOList);
//            return tableInfoVO;
//        }).collect(Collectors.toList());
        context.put("tableInfos",tableInfos);
        File frameFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+"/src/main/resources/templates/frame");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_BOOTSTRAP_LAYOUT,frameFile.getPath()+"/layout.html");
        
        File staticFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+"/src/main/resources/static");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_BOOTSTRAP_INDEX,staticFile.getPath()+"/index.html");
        
        //生成静态文件页面
        File htmlFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+"/src/main/resources/static/tables");
        final String tables = htmlFile.getPath();
        if(!tableInfos.isEmpty()){
        	tableInfos.stream().forEach(it->{
                context.put("table",it);
                try {
					GIOUtils.vmToFile(context, PathConstants.TEMPLATE_BOOTSTRAP_TABLE, tables + "/" + it.getEntityName() + ".html");
				} catch (IOException e) {
					e.printStackTrace();
				}
            });
        }
        
        //生成thymleaf页面
        File thymleafFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+"/src/main/resources/templates/views");
        File topnavFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+"/src/main/resources/templates/topnav");
        final String views = thymleafFile.getPath();
        final String topnav = topnavFile.getPath();
        if(!tableInfos.isEmpty()){
        	tableInfos.stream().forEach(it->{
                context.put("table",it);
                try {
					GIOUtils.vmToFile(context, PathConstants.TEMPLATE_THYMLEAF_FRAME, views + "/" + it.getEntityName() + ".html");
					GIOUtils.vmToFile(context, PathConstants.TEMPLATE_THYMLEAF_TOPNAV, topnav + "/" + it.getEntityName() + ".html");
				} catch (IOException e) {
					e.printStackTrace();
				}
            });
        }
    }
}
